Ext.apply(Ext.form.field.VTypes, {
	company:function(val,field){
		var id = field.ownerCt.down('field[name=id]').getValue(0);
		var rs = false;
		Ext.Ajax.request({
		    url: 'enterprise/companyIsSure.do',
		    async: false,
		    params: {
		    	eid: id,
		    	name:val
		    },
		    success: function(response){
		        var d = response.responseText;
		        rs = (d == 0);
		    }
		});
		
		 return rs;
	},
	companyText:'部门名称已存在，请重新输入！',
	department:function(val,field){
		var eid = field.ownerCt.down('field[name=eid]').getValue(0);
		var id = field.ownerCt.down('field[name=id]').getValue(0);
		Ext.Ajax.request({
		    url: 'enterprise/departmentIsSure.do',
		    async: false,
		    params: {
		    	eid: eid,
		    	id: id,
		    	name:val
		    },
		    success: function(response){
		        var d = response.responseText;
		        rs = (d == 0);
		    }
		});
		
		 return rs;
	},
	departmentText: '部门已存在，请重新输入！'
});



Ext.define('Module.System.DeptManager', {
	extend : 'Ext.panel.Panel',
	alias : 'widget.enterprisemanager',
	layout: 'fit',
	border: false,
	initComponent : function() {
		var me = this;
		var tree =  Ext.create('Ext.tree.Panel',{
			title: '国网新疆电力公司',
			region: 'west',
			rootVisible: true,
			width: 200,
			region: 'west',
			store: Ext.create('Ext.data.TreeStore', {
				fields: ['id','pid','name','enable','remark','uptime','crtime','upuser'],
		        proxy: {
		            type: 'ajax',
		            url: 'enterprise/getEnterprise.do'
		        },
		        root: {
		        	iconCls:'icon-group',
		            id: '0',
		            name:'国网新疆乌鲁木齐供电公司',
		            expanded: true
		        }
		    }),
		    tbar: [{
				text: '添加部门',
				iconCls: 'icon-add',
				handler: function(){
					var rec = tree.getSelectionModel().getSelection()[0];
					if(rec){
						me.addDept(rec,false,'添加部门');
						return ;
					}else if(tree.getRootNode().childNodes < 1){
						me.addDept(tree.getRootNode(),false,'添加部门');
						return ;
					}
					else{
						Ext.Msg.alert('提示', '请选择一个部门！');
					}
				}
			},{
				text: '修改部门',
				iconCls: 'icon-edit',
				handler: function(){
					var rec = tree.getSelectionModel().getSelection()[0];
					if(rec){
						if(rec.raw.name == '国网新疆乌鲁木齐供电公司' && rec.raw.id == 0){
							Ext.Msg.alert('提示', '根目录不能编辑！');
							return;
						}
						
						me.addDept(rec,true,'修改部门');
						Ext.getCmp(me.id+'-deptWin').setIconCls('icon-edit');
					}
					else{
						Ext.Msg.alert('提示', '请选择一个部门！');
					}
				}
			},{
				text: '删除部门',
				iconCls: 'icon-delete',
				handler: function(){
					var rec = tree.getSelectionModel().getSelection()[0];
					if(rec){
						if(rec.raw.name == '国网新疆乌鲁木齐供电公司' && rec.raw.id == 0){
							Ext.Msg.alert('提示', '根目录不能删除！');
							return;
						}
						me.delDept(rec);
					}
					else{
						Ext.Msg.alert('提示', '请选择一个部门！');
					}
				}
			}],
			columns: [{
				xtype: 'treecolumn', 
	            text: '部门名称',
	            flex: 1,
	            sortable: false,
	            dataIndex: 'name'
			},{
	            text: '备注',
	            flex: 2,
	            sortable: false,
	            dataIndex: 'remark'
			}],
		    listeners: {/*
		    	select: function(model,rec){
		    		var p = me.items.items[1];
		    		var layout = p.getLayout();
		    		var formp = p.items.items[rec.data.pid == 0 ? 0 : 1];
		    		formp.loadRecord(rec);
		    		layout.setActiveItem(formp);
		    		formp.setTitle(rec.data.name+'---信息');
		    	}
		    */}
		});
		
		me.items = [tree];
		
		
		me.callParent();
	},
	delDept: function(rec){
		var me = this;
		var id = rec.data.id;
		var tree = this.items.items[0];
		var formp = me.items.items[1];
		if(id && '' != id){
			Ext.Msg.confirm('提示', '你确定要删除当前部门吗？',function(b){
				if('yes' == b){
					Ext.Ajax.request({
					    url: 'enterprise/deleteEnterprise.do',
					    params: {
					        id: id,
					        deptname:rec.data.name
					    },
					    success: function(response){

        	    	    	var rs = Ext.decode(response.responseText);
        	    	    	if(rs.rst){
        	    	    		Ext.Msg.alert('提示', '删除成功！');
//    					    	formp.items.items[0].form.reset();
//    					    	formp.items.items[1].form.reset();
    					    	tree.refresh();
    					    	tree.getSelectionModel().deselectAll(true);
        	    	    	}
        	    	    	else{
        	    	    		Ext.Msg.alert('提示', '删除失败！');
        	    	    	}
					    }
					});
				}
			});
		}
		else{
			Ext.Msg.alert('提示', '请选择要删除的部门！');
		}
	},
	addDept: function(rec,edit,titleName){
		var me = this;
		var tree = this.items.items[0];
		var values = {
			id: edit ? rec.data.id : '',
			eid: edit ? rec.data.eid : (rec.data.pid == 0 ? rec.data.id : rec.data.eid),
	        name: edit ? rec.data.name : '',
	        pname:rec.data.name,
	        pid: edit ? rec.data.pid : rec.data.id,
	        remark: edit ?rec.data.remark:'',
	        profile: edit ? rec.data.profile : ''
		};
		
		Ext.create('Ext.window.Window',{
			width: 500,
			height: edit?345:368,
			id:me.id+"deptId",
			iconCls: 'icon-add',
			id:me.id+'-deptWin',
			modal: true,
			layout: 'fit',
			title: titleName,
			buttons: [{
				text: '保存',
				handler: function(){
					var formp = this.up('panel').items.items[0];
        	    	var form = formp.form;
        	    	if(form.isValid()){
        	    		form.submit({
            	    	    url: 'enterprise/addEnterprise.do',
            	    	    success: function(form, action) {
            	    	    	var rs = Ext.decode(action.response.responseText);
            	    	    	if(rs.rst){
            	    	    		Ext.Msg.alert('提示', '保存成功！');
            	    	    		formp.up('window').close();
//            	    	    		me.items.items[0].refresh();
            	    	    		me.items.items[0].store.load({callback: function(){
            	    	    			rs.msg;
            	    	    			var newnode = tree.getRootNode().findChild('id',rs.msg,true);
            	    	    			tree.getSelectionModel().select(newnode);
            	    	    			while(true){
            	    	    				newnode.parentNode.expand();
            	    	    				if(newnode.parentNode.data.id == 0)break;
            	    	    				newnode = newnode.parentNode;
            	    	    			}
            	    	    		}});
            	    	    		if(rec){
            	    	    			me.query('form')[1].form.setValues(form.getValues());
            	    	    		}
//            	    	    		var newnode = tree.getRootNode().findChild('id',rs.msg,true);?
//            	    	    		alert(newnode);
            	    	    	}
            	    	    	else{
            	    	    		Ext.Msg.alert('提示', '保存失败！详情：<br>'+rs.msg);
            	    	    	}
            	    	    }
            	    	});
        	    	}
        	    	else{
        	    		Ext.Msg.alert('提示', '无效的部门信息,请正确填写部门信息！');
        	    	}
				}
			},{
				text: '取消',
				handler: function(){
					this.up('window').close();
				}
			}],
			items: me.getDeptForm({
				bodyPadding: 20,
				border: false,
				listeners: {
					afterrender: function(fp){
						fp.form.setValues(edit?rec.raw:values);
					}
				}
			},edit)
		}).show();
	},
	getForm: function(config,readonly){
		return Ext.apply({
			xtype: 'form',
			bodyPadding: 10,
			defaults: {width: 450,xtype: 'fieldset',layout: 'column'},
			items: [{
				title: '部门基本信息',
				defaults: {columnWidth: 1,padding: '0 0 5 0'},
				items: [{
					name: 'id',
					xtype: 'hidden',
					allowBlank: true
				},{
					fieldLabel: '父目录',
					name: 'pid',
					xtype: 'hidden',
					allowBlank: false
				},{
					fieldLabel: '部门名称',
					name: 'name',
					xtype: 'textfield',
					vtype: readonly ? '' : 'company',
					allowBlank: false
				}]
			},{
				title: '部门简介',
				defaults: {columnWidth: 1,padding: '0 0 5 0'},
				items: [{
					name: 'profile',
					height: 120,
					maxLength:500,
					xtype: 'textarea'
				}]
			}]
		},config);
	},
	getDeptForm: function(config,edit){
		return Ext.apply({
			xtype: 'form',
			bodyPadding: 30,
			defaults: {width: 450,xtype: 'fieldset',layout: 'column'},
			items: [{
				title: '部门基本信息',
				defaults: {columnWidth: 1,padding: '0 0 5 0',allowBlank: false},
				items: [{
					fieldLabel: '部门ID',
					name: 'id',
					xtype: 'hidden'
				},{
					fieldLabel: '部门pid',
					name: 'pid',
					xtype: 'hidden'
				},{
					fieldLabel: 'eid',
					name: 'eid',
					xtype: 'hidden'
				},{
					fieldLabel: '上级部门',
					allowBlank: true,
					name: 'pname',
					xtype: 'textfield',
					readOnly:true,
					hidden:edit?true:false	
				},{
					fieldLabel: '部门名称',
					regex: /^[a-zA-Z0-9\u4e00-\u9fa5]+$/,
					regexText : '不能输入特殊字符',
					name: 'name',
					xtype: 'textfield',
					allowBlank: false,
					maxLength:16,
					vtype: 'department'
				}]
			},{
				title: '部门备注',
				defaults: {columnWidth: 1,padding: '0 0 5 0'},
				items: [{
					name: 'remark',
					height: 120,
					xtype: 'textarea',
					maxLength:120
				}]
			}]
		},config);
	}
});